// pages/topic/rank.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topicData:{},
    page:1,
    page2:1,
    tabId:0,
    lists:[],
    list1:[],
    list2:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.topic_id = options.id ? options.id :'83642';
    this.getTopic();
    var user_info = wx.getStorageSync('user_info');
    this.setData({ 'user_info': user_info });  
    this.indexTab();
    console.log(user_info);
  },
  getTopic(){
    var that = this;
    //this.data.loading=1;
    app.api('topic/detail', {
      fields: "user,id,title,user_id,user,user_join,topic_join.days,user_count,topic_join.rank",
      id: that.data.topic_id
    }, function (res) {
      //this.data.loading=0;
      if (res.code != 200) {
        app.alert(res.msg);
        return false;
      }
      var topicData = res.data;
      topicData.p = ((topicData.user_join - topicData.rank + 1) / topicData.user_join) * 100;
      that.setData({ topicData: res.data });
    }); 
  },
  indexTab(e){
    if (e){
      var tabId=(e.currentTarget.dataset.id);
      this.setData({ tabId: tabId});
    }
    console.log(this.data);
     if(this.data.tabId==0){
       this.setData({ lists: this.data.list1 });
       this.getRankWeek(); 
     }else{
       this.setData({ lists: this.data.list2 });
       this.getRank();
     }
  },
  getRankWeek(){
    var that = this;
    if (this.data.loading2 == 1 || this.data.lastpage2 == 1) {
      return false;
    }
    this.data.loading2 = 1;
    app.api('topicjoin/rank', {
      fields: "SnsTopic.user,id,days",
      topic_id: that.data.topic_id,
      order: "days desc",
      page: that.data.page2,
      limit: 50,
    }, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return false;
      }
      if (res.data.list.length == 0) {
        that.data.lastpage2 = 1;
      }
      // console.log(that.data.lists);
      that.setData({ list1: that.data.list1.concat(res.data.list) });
      that.data.page2++;
      that.setData({ lists: that.data.list1});
    });
  },
  getRank(){
    var that = this;
    if(this.data.loading == 1 || this.data.lastpage==1){
      return false;
    }
    this.data.loading=1;
    app.api('topicjoin/get', {
      fields:"SnsTopicJoin.user,SnsTopicJoin.topic_id,SnsTopicJoin.days,SnsTopicJoin.id",
      topic_id: that.data.topic_id,
      order:"SnsTopicJoin.days desc",
      page: that.data.page,
      limit:50,
    }, function (res) {
      that.data.loading=0;
      if (res.code != 200) {
        app.alert(res.msg);
        return false;
      }
      if (res.data.list.length == 0) {
        that.data.lastpage = 1;
      }
     // console.log(that.data.lists);
      that.setData({ list2: that.data.list2.concat(res.data.list) });
      that.data.page++;
      that.setData({ lists: that.data.list2 });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getRank();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    var title = "我在 “" + app.util.cutString(that.data.topicData.title, 20) + "“ 中击败了" + that.data.topicData.p+"% 的人";
    return {
      title: title ,
      path: '/pages/topic/detail?id=' + that.data.topicData.id,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  make_image: function () {
    //生成海报
    wx.navigateTo({
      url: '/pages/draw/rank?id=' + this.data.topic_id,
    })
  },
})